/**
 *部门js 
 **/

/**
 * 添加部门
 **/
function savePonDepart(data){
	pdb.transaction(function (tx) {
    	tx.executeSql("INSERT INTO DEPART (ID,NAME,SORT,STATUS,CODE,PID,OID) VALUES (?,?,?,?,?,?,?)",
    			[data.id==undefined ? '' :data.id,data.text==undefined ? '' :data.text,data.sort==undefined ? '' :data.sort,
    			 data.status==undefined ? '' :data.status,data.code==undefined ? '' :data.code,data.pid==undefined ? '' :data.pid,
    			 data.organId==undefined ? '' :data.organId],
       function (tx, rs) {
       },
	   function (tx, error) {
    	   pMessage("添加部门数据失败");
	   });
    });
}

/**
 * 通过ID获取部门
 **/
function findPonDepartById(id,callback){
	pdb.transaction(function (tx) { 
		tx.executeSql('SELECT * FROM DEPART WHERE ID = ?', [id], function (tx, results){ 
		    var len = results.rows.length, i;
		    var data = {};
		    for (i = 0; i < len; i++){
		    	data.id = results.rows.item(i).ID;
		    	data.name = results.rows.item(i).NAME;
		    	data.sort = results.rows.item(i).SORT;
		    	data.status = results.rows.item(i).STATUS;
		    	data.code = results.rows.item(i).CODE;
		    	data.pid = results.rows.item(i).PID;
		    	data.oid = results.rows.item(i).OID;
		    }
			if (typeof(callback) == 'function'){
				callback(data);
			}
	    }, 
	    function(tx, ex) {
	    	pMessage("获取部门失败");
        }); 
	});
}

/**
 * 查询部门数据
 **/
function ponDepart(id,callback){
	pdb.transaction(function (tx) { 
		tx.executeSql('SELECT * FROM DEPART WHERE ID=？', [id], 
		function (tx, results){
			var len = results.rows.length;
			var data = null;
		    for (var i = 0; i < len; i++){
		    	data = results.rows.item(i).ID;
		    }
		    if(null == data){
		    	getPonDepartData();
		    }
			if (typeof(callback) == 'function'){
				callback(data);
			}
		});  
	});
}

/**
 * 从服务器上获取部门所有数据.
 **/
function getPonDepartData(){
	$.ajax({
		type : "post",
		url : getContextPath() + "/depart/findAll",
		dataType : "json",
		error : function(request) {
			pMessage("获取部门列表数据失败");
		},
		success : function(data) {
			$.each(data, function(i, item) {
				/**
				 * 添加到本地
				 **/
				savePonDepart(item);
			});
			pMessage("获取部门列表数据成功");
		}
	});
}